import 'package:flutter/material.dart';
import 'package:flutter_app_demo/constants/constants.dart';
import 'package:flutter_app_demo/pages/discovery_page.dart';
import 'package:flutter_app_demo/pages/news_list_page.dart';
import 'package:flutter_app_demo/pages/profile_page.dart';
import 'package:flutter_app_demo/pages/tweet_page.dart';
import 'package:flutter_app_demo/widget/my_drawer.dart';
import 'package:flutter_app_demo/widget/navigation_icon_view.dart';

//学习布局构造
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _appBarTitle = ["咨询", "动弹", "发现", "我的"];
  List<NavigationIconView> _navigationIconView;
  var _currentIndex = 0;

  List<Widget> _pages;

  PageController _pageController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _navigationIconView = [
      NavigationIconView(title: "咨询", iconPath: Icons.home),
      NavigationIconView(title: "动弹", iconPath: Icons.accessibility_new_sharp),
      NavigationIconView(title: "发现", iconPath: Icons.find_replace_outlined),
      NavigationIconView(title: "我的", iconPath: Icons.my_library_add),
    ];

    _pages = [
      NewsListPage(),
      TweetPage(),
      DiscoveryPage(),
      ProfilePage(),
    ];
    _pageController = PageController(initialPage: -_currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //标题
      appBar: AppBar(
        elevation: 1.0, //阴影
        title: Text(
          "开源中国",
          style: TextStyle(color: Color(AppColors.APPBAR)),
        ),
        iconTheme: IconThemeData(color: Color(AppColors.APPBAR)),
      ),
      body: PageView.builder(
        // 禁止滑动
        // physics: NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          return _pages[index];
        },
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        items: _navigationIconView.map((e) => e.item).toList(),
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          print("onTab: $index");
          setState(() {
            _currentIndex = index;
          });
          _pageController.animateToPage(index,
              duration: Duration(milliseconds: 300), curve: Curves.easeIn);
        },
      ),
      //抽屉
      //drawer: Drawer(),
      drawer: MyDrawer(
        headImgPath: 'images/ycy1.jpg',
        menuIcons: [
          Icons.send,
          Icons.home,
          Icons.error,
          Icons.settings,
        ],
        menuTitles: ["发布动弹", "动弹小黑屋", "关于", "设置"],
      ),
    );
  }
}
